#!/bin/bash
# 简单的监控面板

set -e

echo "🎛️  Browse ETL 监控面板"
echo "================================"
echo ""

# LocalStack状态
echo "1️⃣ LocalStack服务状态："
echo "---"
curl -s http://localhost:4566/_localstack/health | python3 -c "
import sys, json
data = json.load(sys.stdin)
services = data['services']
running = [k for k, v in services.items() if v == 'running']
print(f'✅ 运行中的服务 ({len(running)}): {', '.join(running)}')
print(f'版本: {data['version']} ({data['edition']})')
"

echo ""
echo "2️⃣ Kinesis流状态："
echo "---"
docker exec browse-etl-localstack awslocal kinesis describe-stream --stream-name click-events-stream --query 'StreamDescription.{Name:StreamName,Status:StreamStatus,Shards:length(Shards)}' --output table 2>&1 || echo "❌ Kinesis流未创建或已删除"

echo ""
echo "3️⃣ Lambda函数状态："
echo "---"
docker exec browse-etl-localstack awslocal lambda get-function --function-name click-event-handler --query '{Name:Configuration.FunctionName,Runtime:Configuration.Runtime,State:Configuration.State,LastModified:Configuration.LastModified}' --output table 2>&1 || echo "❌ Lambda函数未创建"

echo ""
echo "4️⃣ API Gateway状态："
echo "---"
docker exec browse-etl-localstack awslocal apigateway get-rest-apis --query 'items[*].{Name:name,ApiId:id,CreatedDate:createdDate}' --output table 2>&1 || echo "❌ API Gateway未创建"

echo ""
echo "5️⃣ PostgreSQL数据统计："
echo "---"
if docker ps | grep -q browse-etl-postgres; then
  docker exec browse-etl-postgres psql -U postgres -d behavior_analysis -t -c "
    SELECT 
      '总记录数: ' || COUNT(*) || ' 条' as stats
    FROM user_behaviors;
  " 2>/dev/null || echo "⚠️  数据库表尚未创建"
else
  echo "⚠️  PostgreSQL容器未运行（未来会用到）"
fi

echo ""
echo "================================"
echo "📊 监控工具："
echo "  - ./scripts/monitor-kinesis.sh   # 实时监控Kinesis"
echo "  - ./scripts/query-postgres.sh    # 查询数据库"
echo "  - ./scripts/view-lambda-logs.sh  # 查看Lambda日志"

